package server;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.security.MessageDigest;
import java.sql.SQLException;

import server.Admin;

public class serverStart extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JTextField TextField_LogID;
	private JPasswordField TextField_Password;
	private JButton button_log;

	public static void main(String[] args){
		serverStart logFrame=new serverStart("虚拟城市后台管理登录");
		
		
		logFrame.setVisible(true);

		
	}

public serverStart(String title) {
	super(title);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setBounds(100, 100, 450, 300);
	setLocationRelativeTo(null);
	contentPane = new JPanel();
	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	setContentPane(contentPane);
	contentPane.setLayout(null);
	
	TextField_LogID = new JTextField();
	TextField_LogID.setBounds(200, 64, 130, 21);
	contentPane.add(TextField_LogID);
	TextField_LogID.setColumns(10);
	
	TextField_Password = new JPasswordField();
	TextField_Password.setBounds(200, 114, 130, 21);
	contentPane.add(TextField_Password);
	TextField_Password.setColumns(10);
	
	JLabel label_name = new JLabel("\u7528\u6237\u540D\uFF1A");
	label_name.setBounds(108, 64, 54, 15);
	contentPane.add(label_name);
	
	JLabel label_password = new JLabel("\u5BC6  \u7801\uFF1A");
	label_password.setBounds(108, 117, 54, 15);
	contentPane.add(label_password);
	
	button_log = new JButton("\u767B\u5F55");
	button_log.setBounds(178, 189, 74, 23);
	contentPane.add(button_log);
	button_log.addActionListener(this);
}
	
public void actionPerformed(ActionEvent event)
{
	if(event.getSource()==button_log)
	{
		String username=TextField_LogID.getText().toString();
		String password=TextField_Password.getText().toString();
		Admin admin=new Admin();
		MD5 md5Encode=new MD5();

		if(username.equals("") || password.equals("")){
			JOptionPane.showMessageDialog(null, "用户名或密码不能为空！");
		}else if(admin.loginConfirm(username,md5Encode.md5(password))){
			dispose();

			RmiServer.main(null);
			serverMainFrame frame = null;
			try {
				frame = new serverMainFrame();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			frame.setLocationRelativeTo(null);
			frame.setVisible(true);
		}else{
			JOptionPane.showMessageDialog(null, "登录失败！");
			this.repaint();
		}
	}
}

}

class MD5{

    //鍗佸叚杩涘埗涓嬫暟瀛楀埌瀛楃鐨勬槧灏勬暟缁�
    private final static String[] hexDigits =

{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};


/**鎶奿nputString鍔犲瘑*/ 
public static String md5(String inputString){ 
return encodeByMD5(inputString); 
}

/** 
* 楠岃瘉杈撳叆鐨勫瘑鐮佹槸鍚︽纭�
* @param password 鐪熸鐨勫瘑鐮侊紙鍔犲瘑鍚庣殑鐪熷瘑鐮侊級 
* @param inputString 杈撳叆鐨勫瓧绗︿覆 
* @return 楠岃瘉缁撴灉锛宐oolean绫诲瀷 
*/ 
public static boolean compare(String md5Str,String inputString){ 
if(md5Str.equals(encodeByMD5(inputString))){ 
return true; 
}else{ 
return false; 
} 
}

/**瀵瑰瓧绗︿覆杩涜MD5缂栫爜*/ 
private static String encodeByMD5(String originString){ 
    if(originString != null){ 
    try{ 
    //鍒涘缓鍏锋湁鎸囧畾绠楁硶鍚嶇О鐨勪俊鎭憳瑕�
    MessageDigest md = MessageDigest.getInstance("MD5"); 
    //浣跨敤鎸囧畾鐨勫瓧鑺傛暟缁勫鎽樿杩涜鏈�悗鏇存柊锛岀劧鍚庡畬鎴愭憳瑕佽绠�
    byte[] results = md.digest(originString.getBytes()); 
    //灏嗗緱鍒扮殑瀛楄妭鏁扮粍鍙樻垚瀛楃涓茶繑鍥�
    String resultString = byteArrayToHexString(results); 
    return resultString.toUpperCase(); 
    }catch(Exception ex){ 
        ex.printStackTrace(); 
       } 
    } 
      return null; 
}

/** 
* 杞崲瀛楄妭鏁扮粍涓哄崄鍏繘鍒跺瓧绗︿覆 
* @param b 瀛楄妭鏁扮粍 
* @return 鍗佸叚杩涘埗瀛楃涓�
*/ 
private static String byteArrayToHexString(byte[] b){ 
StringBuffer resultSb = new StringBuffer(); 
for(int i=0;i<b.length;i++){
resultSb.append(byteToHexString(b[i])); 
} 
return resultSb.toString(); 
}

/** 
* 灏嗕竴涓瓧鑺傝浆鍖栨垚鍗佸叚杩涘埗褰㈠紡鐨勫瓧绗︿覆 
*/ 
private static String byteToHexString(byte b){ 
int n = b; 
if(n<0) 
n=256+n; 
int d1 = n/16; 
int d2 = n%16; 
return hexDigits[d1] + hexDigits[d2]; 
}
}